package santiago.view.lobby;

import java.awt.Color;
import java.awt.Component;
import java.util.Collection;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import santiago.client.Client;
import santiago.domain.ColorType;
import santiago.domain.Player;
import santiago.view.SantiagoDialog;

public class DialogJoinGame extends SantiagoDialog {

    private String GameName;

    /**
     * Creates new form DialogJoinGame
     */
    public DialogJoinGame(java.awt.Frame Parent, boolean Modal, String GameName) {
        super(Parent, Modal);
        this.GameName = GameName;
        initComponents();
        LabelStartInit.setVisible(false);
        setLocationRelativeTo(null);
    }

    @Override
    public void refresh() {
        TablePlayers = new JTable() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        TablePlayers.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable Table, Object Value, boolean IsSelected, boolean HasFocus, int Row, int Column) {
                Component Cell = super.getTableCellRendererComponent(Table, Value, IsSelected, HasFocus, Row, Column);
                Color C;
                if ((C = ColorType.getColor(Value.toString())) != null) {
                    Cell.setForeground(C);
                    Cell.setBackground(C);
                } else {
                    Cell.setForeground(Color.BLACK);
                    Cell.setBackground(Color.WHITE);
                }
                return Cell;
            }
        });
        TablePlayers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        String[][] Data = null;
        String Col[] = {"Joueur", "Couleur"};
        DefaultTableModel Model = new DefaultTableModel(Data, Col);
        Collection<Player> CurrentPlayers = Client.getPlayers();
        int MaximumNumberOfPlayers = Client.getGame().getParameters().getPlayerCount();
        int i = 0;
        for (Player Player : CurrentPlayers) {
            Model.addRow(new String[]{Player.getName(), Player.getColor().toString()});
            i++;
        }
        while (i != MaximumNumberOfPlayers) {
            Model.addRow(new String[]{"En attente...", "En attente.."});
            i++;
        }
        TablePlayers.setModel(Model);
        ScrollPaneTableContainer.getViewport().removeAll();
        ScrollPaneTableContainer.getViewport().add(TablePlayers);
    }

    public void initStart() {
        ButtonQuitGame.setVisible(false);
        LabelStartInit.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabelTitle = new javax.swing.JLabel();
        LabelInfo = new javax.swing.JLabel();
        ScrollPaneTableContainer = new javax.swing.JScrollPane();
        TablePlayers = new javax.swing.JTable();
        ButtonQuitGame = new javax.swing.JButton();
        LabelStartInit = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Partie " + GameName + " en cours de préparation");
        setResizable(false);

        LabelTitle.setFont(new java.awt.Font("Lucida Sans", 1, 18)); // NOI18N
        LabelTitle.setForeground(new java.awt.Color(179, 96, 74));
        LabelTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelTitle.setText("Partie " + GameName);

        LabelInfo.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelInfo.setText("La partie sera lancée automatiquement lorsque le nombre de joueur sera atteint");

        TablePlayers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nom du joueur", "Couleur"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ScrollPaneTableContainer.setViewportView(TablePlayers);

        ButtonQuitGame.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonQuitGame.setForeground(new java.awt.Color(179, 96, 74));
        ButtonQuitGame.setText("Quitter la partie");
        ButtonQuitGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonQuitGameActionPerformed(evt);
            }
        });

        LabelStartInit.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelStartInit.setForeground(new java.awt.Color(255, 0, 0));
        LabelStartInit.setText("La partie va commencer...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(LabelStartInit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ButtonQuitGame))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LabelInfo)
                            .addComponent(ScrollPaneTableContainer, javax.swing.GroupLayout.PREFERRED_SIZE, 547, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(20, Short.MAX_VALUE))
            .addComponent(LabelTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(LabelTitle)
                .addGap(18, 18, 18)
                .addComponent(LabelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ScrollPaneTableContainer, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonQuitGame)
                    .addComponent(LabelStartInit, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(603, 325));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonQuitGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonQuitGameActionPerformed
        Client.leaveGame();
    }//GEN-LAST:event_ButtonQuitGameActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonQuitGame;
    private javax.swing.JLabel LabelInfo;
    private javax.swing.JLabel LabelStartInit;
    private javax.swing.JLabel LabelTitle;
    private javax.swing.JScrollPane ScrollPaneTableContainer;
    private javax.swing.JTable TablePlayers;
    // End of variables declaration//GEN-END:variables
}
